package com.inviscidlabs.schooled;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

public class FragmentSemesterEdit extends Fragment{

	
//Variables
	private EditText field_SemesterName;
	private GradeDB dB;
	private Cursor c;
	
//Interface
	private SemEditListener semEditListener;
	
//===================ON CREATE VIEW======================	
		 @Override
		 public View onCreateView(LayoutInflater inflater, ViewGroup container,
		      Bundle savedInstanceState) {
		 View v= inflater.inflate(R.layout.screen_semesteredit, container);
		 field_SemesterName=(EditText) v.findViewById(R.id.semedit_field_SemesterName);
		 return v;
		 }
		 
//===================LOADSEMESTER===================
		 
		 public void loadSemester(long sId){
			 c=dB.fetchSemester(sId);
			 field_SemesterName.setText(c.getString(c.getColumnIndexOrThrow(GradeDB.KEY_SEMNAME)));			 
		 }
		 
//===================SAVESEMESTER================
		 
		 public void saveSemester(){
			 
			 String semNameContent=field_SemesterName.getText().toString();
			 if(semNameContent.matches("")){
				 semEditListener.dialogNothingInSemField();
				 Toast.makeText(getActivity(), "if condition satisfied", Toast.LENGTH_LONG).show();

			 } else {
			 dB.createSemester(semNameContent);
			 
			 }
			 }
		 
//==============UPDATE SEMESTER=====================
		 public void updateSemester(long sId){
			 String semNameContent=field_SemesterName.getText().toString();
			 if(semNameContent==null||semNameContent==""){
				 semEditListener.dialogNothingInSemField();
			 } else {
				 dB.updateSemester(sId, semNameContent);
			 }
		 }
	
//==============DELETE SEMESTER====================
		 
		 public void deleteSemester(long sId){
			 dB.deleteSemester(sId);
			 
		 }
		
		 
//===================ON ATTACH===================
		 @Override
		  public void onAttach(Activity activity) {
		    super.onAttach(activity);
		   dB=GradeDB.getInstance(getActivity());
		   if (activity instanceof SemEditListener){
			   semEditListener=(SemEditListener) getActivity();
		   } else {
			   throw new ClassCastException(activity.toString()+"must implement SemEditListener");  
		   }
		 
		 }
		 
//==================ON SEMEDITACTION===============
		 public interface SemEditListener{
			 public void dialogNothingInSemField();
		 }

}

